package com.mano.mall.service.mapper;

import com.mano.mall.service.mapper.entity.SeckillEntity;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

public interface SeckillMapper {

	/**
	 *  容易超卖，最简单的库存扣减
	 * @param seckillId
	 * @return
	 */
	@Update("update mano_seckill set inventory=inventory-1 where  seckill_id='10001';")
	int inventoryDeduction(Long seckillId);

	/**
	 * 使用乐观锁修改库存信息 and inventory>0方式
	 *
	 * @param seckillId
	 * @return
	 */
	@Update("update mano_seckill set inventory=inventory-1 where  seckill_id='10001' and inventory>0")
	int optimisticLockSeckill(Long seckillId);

	/**
	 * 基于版本号形式实现乐观锁
	 *
	 * @param seckillId
	 * @return
	 */
	@Update("update mano_seckill set inventory=inventory-1 ,version=version+1 where  seckill_id=#{seckillId} and version=#{version} and inventory>0;")
	int optimisticVersionSeckill(@Param("seckillId") Long seckillId, @Param("version") Long version);


	/**
	 *  获取秒杀的商品和库存信息
 	 * @param seckillId
	 * @return
	 */
	@Select("SELECT seckill_id AS seckillId,name as name,inventory as inventory,start_time as startTime,end_time as endTime,create_time as createTime,version as version from mano_seckill where seckill_id=#{seckillId}")
	SeckillEntity findBySeckillId(Long seckillId);

}